package com.ddwl.user.dao;


import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.user.dao.mapper.WxUserInfoMapper;
import com.ddwl.user.model.WxUserInfo;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hemiaojun
 */
@Repository
public class WxUserInfoDao extends TkDao<WxUserInfoMapper, WxUserInfo> {

    @Resource
    private WxUserInfoMapper wxUserInfoMapper;


    public List<WxUserInfo> listByOpenIds(List<String> openIds) {
        if (CollectionUtils.isEmpty(openIds)) {
            return new ArrayList<>();
        }
        Example example = getExample();
        example.and().andIn("openid", openIds);
        return wxUserInfoMapper.selectByExample(example);
    }

    public void updateList(List<WxUserInfo> wxUserInfoList) {
        wxUserInfoMapper.updateList(wxUserInfoList);
    }

    public void deleteByOpenId(String fromUserName) {
        Example example = getExample();
        example.and().andEqualTo("openid", fromUserName);
        WxUserInfo update = new WxUserInfo();
        update.setDflag(BaseEnum.DflagEnum.DELETED.getCode());
        update.setGmtModified(Instant.now());
        wxUserInfoMapper.updateByExampleSelective(update, example);
    }
}